<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="Job名称" prop="jobName">
        <el-input v-model="queryParams.jobName" placeholder="请输入Job名称"" clearable size=" small" style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jobList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="Job名称" prop="jobName" width="120" />
      <el-table-column label="Job编码" prop="jobCode" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="Cron表达式" prop="cron" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="分片参数" prop="keyName" width="100" />

      <el-table-column prop="zone" label="分片策略">
        <template slot-scope="scope">
          <span v-for="(v, i) in zoneMethod"  v-if ="scope.row.zone == v.key"> {{v.value}}</span>
        </template>
      </el-table-column>
      <el-table-column label="运行实例数" prop="appCounts" width="100" />
      <el-table-column label="所属JAVA类别" prop="toLong" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span></span>
          <el-dropdown @command="changeItem">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item  icon="el-icon-edit" :command="beforeHandleCommand('handleUpdate', scope.row)">修改实例</el-dropdown-item>
              <el-dropdown-item  icon="el-icon-loading" :command="beforeHandleCommand('lookinstance', scope.row.jobId)">查看实例</el-dropdown-item>
              <el-dropdown-item  icon="el-icon-position" :command="beforeHandleCommand('lookNodeList', scope.row.jobId)">查看运行节点</el-dropdown-item>
              <el-dropdown-item  icon="el-icon-video-play" :command="beforeHandleCommand('startJob', scope.row.jobId)">运行所有节点</el-dropdown-item>
              <el-dropdown-item  icon="el-icon-video-pause" :command="beforeHandleCommand('stopJob', scope.row.jobId)">停止所有节点</el-dropdown-item>
              <!-- <el-dropdown-item  icon="el-icon-plus" :command="beforeHandleCommand('lookJobLogs', scope.row.jobId)">查看实例日志</el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageLimit.pageNum" :limit.sync="pageLimit.pageSize"
      @pagination="getList" />

    <!--新增或者修改-->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form v-model="form" label-width="120px">
        <el-form-item label="执行器名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入执行器名称" />
        </el-form-item>
        <el-form-item label="执行器编码" prop="jobCode">
          <el-input v-model="form.jobCode" placeholder="请输入执行器编码" />
        </el-form-item>
        <el-form-item label="Cron表达式" prop="cron">
          <el-input v-model="form.cron" placeholder="请输入Cron表达式" />
        </el-form-item>
        <el-form-item label="分片参数" prop="keyName">
          <el-input v-model="form.keyName" placeholder="请输入分片参数" />
        </el-form-item>
        <el-form-item label="分片策略" prop="zone">
              <el-select v-model="form.zone" >
                <el-option
                  v-for="(v, i) in zoneMethod"
                  :label="v.value"
                  :value="v.key"
                  :key="i"/>
              </el-select>
        </el-form-item>
        <el-form-item label="类属" prop="toLong">
          <el-input v-model="form.toLong" placeholder="请输入类属" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <!-- <el-button @click="cancel">取 消</el-button> -->
      </div>
    </el-dialog>

   <!--节点泪飙-->
    <el-dialog title="节点列表" :visible.sync="nodeOpen" width="600px" append-to-body>
      <h3  v-for=" (v,i) in nodeList">{{v}}</h3>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listJob,
    delJob,
    addJob,
    updateJob,
    getZone,
    getNodeList,
    startJob,
    stopJob

  } from "@/api/job/job";
  export default {
    name: "Job",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 角色表格数据
        jobList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        nodeOpen:false,
        nodeList:[],
        // 日期范围
        dateRange: [],
        statusOptions: [],
        // 查询参数
        pageLimit: {
          pageNum: 1,
          pageSize: 10
        },
        queryParams: {},
        // 表单参数
        form: {},
        zoneMethod: [],
        defaultProps: {
          children: "children",
          label: "label"
        },
      };
    },
    created() {

      this.getZoneMethods();
    },
    methods: {

      /**
       * 获取分片策略
       */
      getZoneMethods()  {
        getZone().then(
          response => {
            this.zoneMethod = response.data;
            this.getList();
          }
        );
      },
      /**
       * dropdown点击下拉多参数绑定
       * @param {Object} flag
       * @param {Object} command
       */
      beforeHandleCommand(flag, command) { // flag为传递的参数
        return {
          'flag': flag,
          'command': command
        }
      },
      /**
       * 点击下拉菜单每一项时触发
       */
      changeItem(val) { // val.flag为传递的flag即'publish'等，val.command为传递的formItem
      if (val.flag === 'handleUpdate') {
        this.handleUpdate(val.command);
      }
        if (val.flag === 'lookinstance') {
          this.lookInstance(val.command);
        }
        if (val.flag === 'lookNodeList') {
          this.lookNodeList(val.command);
        }
        if (val.flag === 'startJob') {
          this.startJob(val.command);
        }

       if (val.flag === 'stopJob') {
         this.stopJob(val.command);
       }

       if(val.flag === 'lookJobLogs'){
         this.lookJobLogs(val.command)
       }

      },
      /**
       * 查询实例
       * @param {Object} row
       */
      lookInstance(jobId) {
        this.$router.push({
          path: '/job/jobInstances',
          query: {
            "jobId": jobId
          }
        })
      },
      lookJobLogs(jobId){
        this.$router.push({
          path: '/job/jobInstancelog',
          query: {
            "jobId": jobId
          }
        })
      },


      /**
       * 查询节点信息
       */
      lookNodeList(jobId){
        debugger
        getNodeList(jobId).then(
           response => {
             this.nodeList = response.data;
             this.nodeOpen = true;
           }
         );
      },
      startJob(jobId){
        this.$confirm('是否确认开启所有实例?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return startJob(jobId);
        }).then((res) => {
          this.msgSuccess("开启成功"+res.data+"个实例");
          this.getList();
        })
      },
      stopJob(jobId){
        this.$confirm('是否确认关闭所有实例?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return stopJob(jobId);
        }).then((res) => {
          this.msgSuccess("关闭成功"+res.data+"个实例");
          this.getList();
        })
      },

      /** 查询列表 */
      getList() {
        this.loading = true;
        listJob(this.queryParams, this.pageLimit).then(
          response => {
            this.jobList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roleId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

      handleDelete() {},


      // 表单重置
      reset() {
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 新增按钮操作 */
      handleAdd() {
        this.title = "新增执行器";
        this.open = true;
        this.form = {};
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.open = true;
        this.title = "修改执行器";
        this.form = row;
      },



      /** 提交按钮 */
      submitForm: function() {

        if (this.form.jobId != undefined) {
          updateJob(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        } else {
          addJob(this.form).then(response => {
            this.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }

      },


    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>
